-- print(">>Script: player Log Message.")

--[[
脚本功能：
同账号下的任一角色学会了 sharedSpells 里配置的法术，其他角色也会自动学会该法术。
]]

-- 可以账号共享的法术ID
-- 目前都是骑术, 也可以添加其它法术
local sharedSpells = {
	458,459,468,470,471,472,578,579,580,581,3363,5784,6648,6653,6654,6777,6896,
	6897,6898,6899,8394,8395,8980,10789,10793,10795,10796,10798,10799,10873,10969,
	13819,15779,15780,15781,16055,16056,16058,16059,16060,16080,16081,16082,16083,
	16084,17229,17450,17453,17454,17455,17456,17458,17459,17460,17461,17462,17463,
	17464,17465,17481,18363,18989,18990,18991,18992,22717,22718,22719,22720,22721,
	22722,22723,22724,23161,23214,23219,23220,23221,23222,23223,23225,23227,23228,
	23229,23238,23239,23240,23241,23242,23243,23246,23247,23248,23249,23250,23251,
	23252,23338,23509,23510,24242,24252,25953,26054,26055,26056,26656,28828,29059,
	30174,32235,32239,32240,32242,32243,32244,32245,32246,32289,32290,32292,32295,
	32296,32297,32345,33630,33660,34406,34407,34767,34769,34790,34795,34896,34897,
	34898,34899,35018,35020,35022,35025,35027,35028,35710,35711,35712,35713,35714,
	36702,37015,39315,39316,39317,39318,39319,39798,39800,39801,39802,39803,40192,
	41252,41513,41514,41515,41516,41517,41518,42776,42777,43688,43810,43899,43900,
	43927,44151,44153,44317,44744,46197,46199,46628,47037,48025,48027,48778,48954,
	49193,49322,49378,49379,50869,50870,51412,51960,54729,54753,55531,58615,58983,
	59567,59568,59569,59570,59571,59572,59573,59650,59785,59788,59791,59793,59797,
	59799,59802,59804,59961,59976,59996,60002,60021,60024,60025,60114,60116,60118,
	60119,60136,60140,60424,61229,61230,61294,61309,61425,61442,61444,61446,61447,
	61451,61465,61467,61469,61470,61996,61997,62048,63232,63635,63636,63637,63638,
	63639,63640,63641,63642,63643,63796,63844,63956,63963,64656,64657,64658,64659,
	64731,64927,64977,65439,65637,65638,65639,65640,65641,65642,65643,65644,65645,
	65646,65917,66087,66088,66090,66091,66122,66123,66124,66846,66847,66906,66907,
	67336,67466,68056,68057,68187,68188,69395,71342,71810,33388,33391,34090,34091,
	54197,
}

-- 转换为hash表
local sharedSpellMap = {}
for _, spellId in ipairs(sharedSpells) do
	sharedSpellMap[spellId] = true
end
sharedSpells = nil -- 释放内存

-- 登陆时检查技能
local function checkSpells(_, player)
	local accountId = player:GetAccountId()
	local guid = player:GetGUIDLow()
	local sql = string.format("SELECT spell FROM character_spell WHERE guid IN (SELECT guid FROM characters WHERE account=%d and guid!=%d) GROUP BY spell;", accountId, guid)
	local spells = CharDBQuery(sql) -- 同账号下其他角色学到的技能
	if not spells then
		return
	end
	repeat
		local spellId = spells:GetUInt32(0)
		if sharedSpellMap[spellId] and not player:HasSpell(spellId) then
			player:LearnSpell(spellId)
		end
	until not spells:NextRow()
end

RegisterPlayerEvent(3, checkSpells)--PLAYER_EVENT_ON_LOGIN
